<?php
class Item_model extends MY_Model
{
    private $table = 'yy_item';
    public function __construct()
    {
        parent::__construct();
    }
    public function get_cat_by_id($id)
    {
        $this->db->where("id",$id);
        $query=$this->db->get("yy_category");
        return $query->row_array();
    }
    public function get_cat_by_parent_id($parent_id, $fields = " * ")
    {
        $sql = "SELECT {$fields} FROM {$this->table} WHERE parent_id = {$parent_id}";
        $query = $this->db->query($sql);
        return $query->result_array();
    }
    
    public function get_item_list_by_catid($data = array(),$fields = "*",$per_page,$limit)
    {
        $where = " onlinestatus=1 AND isDel=0";

        if($per_page > 1){
            $per_page = ($per_page-1)*$limit+1;
        }else{
            $per_page = 0;
        }
        $limit = " limit {$per_page},{$limit}";
        if(isset($data['categoryid'])){
            $where .= " AND categoryid={$data['categoryid']}";
        }
        $order = "  ORDER BY ";
        if($data['sort'] == 1){
            //降序
            $order .= " referPrice ASC ,";
        }
        if($data['sort'] == 2){
            //降序
            $order .= " referPrice DESC ,";
        }
		$sql = "SELECT {$fields} FROM {$this->table} WHERE {$where}  {$order} addtime DESC {$limit}";
		$query = $this->db->query($sql);
        return $query->result_array();        
    }
    //根据itemid获取商品默认封面
    public function get_default_cover($itemid){
        $sql = "SELECT imgurl FROM yy_item_attach WHERE itemid = {$itemid} AND is_default=1";
        $query = $this->db->query($sql);
        $result = $query->row_array();
        return $result['imgurl'];
    }
  
    /*
     * 获取无限分类ID下面的子类ID集
     *
    */
    public function get_Children_Ids ($cat_id)
    {    
        $ids = "";
		$sql = "SELECT * FROM yy_item_category WHERE parent_id='{$cat_id}'";
		$query = $this->db->query($sql);
        $rs = $query->result_array ();
        if ($rs){ 
            foreach ($rs as $k=>$row){
                $ids .= ','.$row['cat_id'];
                $ids .= $this->get_Children_Ids ($row['cat_id']);
            }            
        }        
        return $ids;
    }   
    
    /**
     * 获取获取当前分类及所有父分类名字
     *@author KevinBai
     */
    public function get_level_catname_bycid($parent_id){
       $sql = "SELECT * FROM yy_item_category WHERE cat_id='{$parent_id}'";
       $query = $this->db->query($sql);
       $tem = $query->row_array();
       $pre_name = "";
       if($query->num_rows()){
           $pre_name.= $this->get_level_catname_bycid($tem['parent_id']); 
           $pre_name.= $tem['cat_name'].'->';   
        }
        return $pre_name;
    }
   public function isCollection($itemid, $fromUsername){
	$sql = "SELECT iscollect from yy_collect where typeid={$itemid} and fromUsername = '{$fromUsername}'";
        $query = $this->db->query($sql);
        return $query->row_array();
	}    
    //根据产品信息获取产品详情
    public function get_item_by_id($id, $fields = " * ")
    {
       $sql = "SELECT {$fields} FROM {$this->table} item WHERE item.itemid = {$id}"; 
	$query = $this->db->query($sql);
        return $query->row_array();
    }   
     //获取图片列表
    public function get_img_list_byid($itemid, $fields = " * "){
        $sql = "SELECT {$fields} FROM yy_item_attach WHERE itemid = {$itemid}";
        $query = $this->db->query($sql);
        return $query->result_array();
    }

    //添加收藏
    public function setCollect($data = array()){
        //检查是否已存在
        $this->db->where(array("typeid"=>$data['typeid'],"fromUsername"=>$data['fromUsername'],"type"=>2));
        $query = $this->db->get("yy_collect");
        $collect_result = $query->row_array();
        $data['iscollect'] = 1;
        if(isset($collect_result['iscollect'])){
            if($collect_result['iscollect'] == 1){
                $data['iscollect'] = 2;
            }
             $this->db->where(array("id"=>$collect_result['id']));
            $res = $this->db->update("yy_collect",$data);
        }else{
            $res = $this->db->insert("yy_collect",$data);
        }
        return $res;
    }

    public function editAttentionRate($itemid){
        $sql = "UPDATE yy_item SET attentionRate=attentionRate+1 WHERE itemid= {$itemid}";
        $res = $this->db->query($sql);
        return $res;
    }
}

/* End of file flybird/orders_model.php */
/* Location: ./application/models/orders_model.php */
